// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 10/23/2005 12:08:38 AM
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3)
// Source File Name:   IvyModuleSettings.java

package net.sf.ivyide.idea;

import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.WriteExternalException;
import fr.jayasoft.ivy.ModuleId;
import net.sf.ivyide.DependencyType;
import net.sf.ivyide.ModuleProviderFactory;
import org.jdom.Attribute;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class IvyProjectSettings
        implements JDOMExternalizable, ProjectComponent {

    public ModuleProviderFactory m_environmentInfo;
    public File m_rootModuleDir;
    private static final String ORGANISATION_NAME = "organisationName";
    private static final String DEPENDENCY = "dependency";
    private static final String DEPENDENCIES = "dependencies";
    private static final String ENVIRONMENT = "environment";
    private static final String CLASS_NAME = "className";
    private static final String DEPENDENCY_TYPE = "dependencyType";
    private static final String MODULE_NAME = "moduleName";
    private static final String ROOT_MODULE_DIR = "rootModuleDir";


    public IvyProjectSettings() {
        m_moduleDependencies = new LinkedHashMap<ModuleId, DependencyType>();
    }

    public void readExternal(Element element)
            throws InvalidDataException {
        Attribute attribute = element.getAttribute(ROOT_MODULE_DIR);
        if (attribute != null) {
            m_rootModuleDir = new File(attribute.getValue());
        }
        Element dependencies = element.getChild(DEPENDENCIES);
        List<Element> children = dependencies.getChildren(DEPENDENCY);
        m_moduleDependencies.clear();
        for (Element dependency : children) {
            ModuleId moduleId = new ModuleId(dependency.getAttribute(ORGANISATION_NAME).getValue(), dependency.getAttribute(MODULE_NAME).getValue());
            DependencyType dependencyType = DependencyType.valueOf(dependency.getAttribute(DEPENDENCY_TYPE).getValue());
            m_moduleDependencies.put(moduleId, dependencyType);
        }
        Element environment = element.getChild(ENVIRONMENT);
        if (environment != null) {
            String className = environment.getAttribute(CLASS_NAME).getValue();
            try {
                m_environmentInfo = (ModuleProviderFactory) ModuleProviderFactory.class.getClassLoader().loadClass(className).newInstance();
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            }
            ((JDOMExternalizable) m_environmentInfo).readExternal(environment);
        }
    }

    public void writeExternal(Element element)
            throws WriteExternalException {
        if (m_environmentInfo != null) {
            Element environment = new Element(ENVIRONMENT);
            environment.setAttribute(CLASS_NAME, m_environmentInfo.getClass().getName());
            ((JDOMExternalizable) m_environmentInfo).writeExternal(environment);
            element.addContent(environment);
        }

        if (m_moduleDependencies != null) {
            Element dependencies = new Element(DEPENDENCIES);
            for (ModuleId moduleId : m_moduleDependencies.keySet()) {
                Element dependency = new Element(DEPENDENCY);
                dependency.setAttribute(MODULE_NAME, moduleId.getName());
                dependency.setAttribute(ORGANISATION_NAME, moduleId.getOrganisation());
                dependency.setAttribute(DEPENDENCY_TYPE, m_moduleDependencies.get(moduleId).name());
                dependencies.addContent(dependency);
            }
            element.addContent(dependencies);
        }
        if (m_rootModuleDir != null) {
            element.setAttribute(ROOT_MODULE_DIR, m_rootModuleDir.getPath());
        }
    }

    public Map<ModuleId, DependencyType> m_moduleDependencies;

    public void copyTo(IvyProjectSettings projectSettings) {
        projectSettings.m_environmentInfo = m_environmentInfo;
        projectSettings.m_rootModuleDir = m_rootModuleDir;
        projectSettings.m_moduleDependencies = m_moduleDependencies;
    }

    public void projectOpened() {
    }

    public void projectClosed() {
    }

    @NotNull
    public String getComponentName() {
        return "IvyProjectSettings";
    }

    public void initComponent() {
    }

    public void disposeComponent() {
    }
}